/*
 * $Id: HandshakeTestServer.java,v 1.3 2009/07/10 19:52:56 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.iptds.worker.test;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import com.ge.healthcare.autosc.processor.iptds.IPTDSProperties;
import com.ge.healthcare.autosc.processor.iptds.IPTDSSocketHandler;

/**
 * 
 * @author 212042946
 * 
 */
public class HandshakeTestServer implements Runnable {

	private ServerSocket ssocket = null;
	private IPTDSProperties iptdsProperties = null;
	private static final String siteName = "DOESNOTMATTER";
	private volatile boolean stop = false;
	private boolean urgent = false;
	private String usn = null;
	private String command = null;

	public HandshakeTestServer(IPTDSProperties iptdsProperties, boolean urgent,
			String usn, String command) throws IOException {
		this.urgent = urgent;
		this.usn = usn;
		this.command = command;
		this.iptdsProperties = iptdsProperties;
		ssocket = new ServerSocket(HandshakeTest.TEST_SOCKET_PORT);
	}

	public void run() {

		System.out.println(this.getClass() + " - Start listening port "
				+ iptdsProperties.getListenPort());
		IPTDSSocketHandler socketHandler = null;
		try {
			Socket socket = ssocket.accept();
			socket.setSoTimeout(10000);
			socketHandler = new IPTDSSocketHandler(socket);
			socketHandler.open();

			if (urgent == true) {
				socketHandler.sendMessage("urgent");
			} else {
				socketHandler.sendMessage("normal");
			}
			String reply = socketHandler.readLine();
			System.out.println(this.getClass() + "- received " + reply);
			if ((reply != null) && (reply.equalsIgnoreCase("begin"))) {
				System.out.println(this.getClass() + "- Send command: "
						+ command);
				socketHandler.sendMessage(command);
				System.out.println(this.getClass() + "- Send USN: " + usn);
				socketHandler.sendMessage(usn);
				System.out.println(this.getClass() + "- Send Sitename: "
						+ siteName);
				socketHandler.sendMessage(siteName);

				while (!stop) {
					System.out.println("before read line " + new Date());
					reply = socketHandler.readLine();
					System.out.println("after read line " + new Date());
					System.out.println(this.getClass() + "- read line: "
							+ reply);
					Thread.sleep(3000);
				}
			} else {
				System.out.println(this.getClass()
						+ "- Expect begin and received " + reply);
			}

		} catch (InterruptedException ie) {
			System.out.println("Get interrupted!");
			Thread.interrupted();
		} catch (Exception e) {
			System.out.println(this.getClass() + "- TestServer get exception");
			e.printStackTrace();
		} finally {
			if (socketHandler != null) {
				socketHandler.close();
			}

			if (!ssocket.isClosed()) {
				try {
					ssocket.close();
				} catch (IOException ioe) {
					ioe.printStackTrace();
					System.out.println(this.getClass()
							+ "- TestServer failed closing server socket");
				}
			}
		}
		System.out.println(this.getClass() + "- End Test Server");
	}

	public void stop() {
		stop = true;
	}
}
